package com.gxa.springboot229.controller;

import com.gxa.springboot229.excption.BusinessException;
import com.gxa.springboot229.pojo.Manager;
import com.gxa.springboot229.service.ManagerService;
import com.gxa.springboot229.util.JWTUtil;
import com.gxa.springboot229.util.JsonResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mgr")
public class ManagerController {
    @Resource
    ManagerService managerService;

    /**
     * 查询接口 查询自己的信息
     * @param map
     * @return
     */
    @PostMapping("/find")
    public Map find(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        Manager manager = new Manager();
        if(map == null || "".equals(map)){
            throw new BusinessException("信息不能为空");
        }
        if(map.get("id")!=null && !"".equals(map.get("id"))){
            int id =(int) map.get("id");
            manager.setId(id);
        }else
        if(map.get("phone")!=null && !"".equals(map.get("phone"))){
            String phone =(String) map.get("phone");
            manager.setPhone(phone);
        }else {
            throw new BusinessException("信息不能为空");
        }
        List <Manager> managerList = managerService.find(manager);
        jsonResult.setCode("1");
        jsonResult.setData(managerList);
        return jsonResult.getValues() ;
    }

    /**
     * 管理员登录
     * @param phone
     * @return
     */
    @PostMapping("/logManager")
    public Map logManager(@RequestParam("phone")String phone){
        String token = null;
        JWTUtil jwtUtil = new JWTUtil();
        Map<String,Object> objectMap =new HashMap<>();
        JsonResult jsonResult = new JsonResult();

        if(phone == null || "".equals(phone)){
            throw new BusinessException("手机不能为空");
        }
        //判断登录是否成功
        if(managerService.Login(phone) == 1){
            List <Manager> my = managerService.findMy(phone);
            jsonResult.setCode("1");
            jsonResult.setMsg("登录成功");
            token = jwtUtil.createJWT(phone, my.get(0).getName(), 30 * 1000 * 60 * 6 * 24, objectMap);
            jsonResult.put("token",token);
            jsonResult.setData(my);
        }else {
            throw new BusinessException("登录失败,您不是管理员");
        }

        return jsonResult.getValues();
    }

    /**
     * 修改接口 只能由超级管理员 修改 管理员 的 基本信息
     * @param map
     * @return
     */
    @PostMapping("/upManager")
    public Map upManager(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        Manager manager = new Manager();
        if(map == null || "".equals(map)){
            throw new BusinessException("数据不能为空");
        }
        if(map.get("id") == null ||"".equals(map.get("id"))){
            throw new BusinessException("id不能为空");
        }
        if(map.get("phone") != null || !"".equals(map.get("phone"))){
             String phone = (String) map.get("phone");
             manager.setPhone(phone);
        }
        if(map.get("name") != null || !"".equals(map.get("name"))){
            String name = (String) map.get("name");
            manager.setName(name);
        }
        int id = (int)map.get("id");
        manager.setId(id);
        int update = managerService.update(manager);
        if(update == 0){
            throw new BusinessException("修改失败");
        }
        int phid = (int) map.get("id");
        List <Manager> my = managerService.findMy(phid);
        jsonResult.setCode("1");
        jsonResult.setData(my);
        return jsonResult.getValues();
    }
}
